package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllComputer;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class ComputerSpezPanel extends JPanel implements ActionListener, ChangeListener {

	private static final long serialVersionUID = 2004661572700282728L;
	private JCheckBox hostName;
	private JCheckBox sid;
	private JCheckBox computerTyp;
	private JCheckBox ram;
	private VisuControllComputer visuControllComputer;
	private int amountOfFlags;

	public ComputerSpezPanel(VisuControllComputer visuControllComputer) {
		this.visuControllComputer = visuControllComputer;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.hostName = new JCheckBox("Hostname", this.visuControllComputer.isHostName());
		this.hostName.addActionListener(this);
		this.hostName.addChangeListener(this);
		this.sid = new JCheckBox("SID", this.visuControllComputer.isSid());
		this.sid.addActionListener(this);
		this.sid.addChangeListener(this);
		this.computerTyp = new JCheckBox("Computer-Typ", this.visuControllComputer.isComputerTyp());
		this.computerTyp.addActionListener(this);
		this.computerTyp.addChangeListener(this);
		this.ram = new JCheckBox("RAM", this.visuControllComputer.isRam());
		this.ram.addActionListener(this);
		this.ram.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.hostName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.sid);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.computerTyp);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());
		y++;
		x = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.ram);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());

	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.hostName.setSelected(flag);
		this.sid.setSelected(flag);
		this.sid.setSelected(flag);
		this.computerTyp.setSelected(flag);
		this.ram.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.hostName) {
			this.visuControllComputer.setHostName(this.hostName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.sid) {
			this.visuControllComputer.setSid(this.sid.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.computerTyp) {
			this.visuControllComputer.setComputerTyp(this.computerTyp.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ram) {
			this.visuControllComputer.setRam(this.ram.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.hostName) {
			this.visuControllComputer.setHostName(this.hostName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.sid) {
			this.visuControllComputer.setSid(this.sid.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.computerTyp) {
			this.visuControllComputer.setComputerTyp(this.computerTyp.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ram) {
			this.visuControllComputer.setRam(this.ram.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getHostNameState() {
		return this.hostName.isSelected();
	}

	public boolean getSidState() {
		return this.sid.isSelected();
	}

	public boolean getComputerTypState() {
		return this.computerTyp.isSelected();
	}

	public boolean getRamState() {
		return this.ram.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.hostName.isSelected()) {
			this.hostName.setEnabled(beEnabledFlag);
		}
		if (!this.sid.isSelected()) {
			this.sid.setEnabled(beEnabledFlag);
		}
		if (!this.computerTyp.isSelected()) {
			this.computerTyp.setEnabled(beEnabledFlag);
		}
		if (!this.ram.isSelected()) {
			this.ram.setEnabled(beEnabledFlag);
		}

	}

}
